<?php

namespace app\award;

use app\models\order\Order;
use app\models\order\OrderItem;
use app\models\product\Product;
use app\models\product\ProductAreaList;
use app\models\SetUserUp;
use app\models\user\User;
use app\models\user\UserAccount;
use app\models\user\UserCommission;
use app\models\user\UserLevel;
use app\services\Chain;

class Up
{
    protected $order;
    protected $set;
    protected $user;

    public function __construct(Order $order)
    {
        $this->order = $order;
        $this->set = SetUserUp::getLastOne();
        $this->user = User::findOne(['id' => $this->order->user_id]);

        $first_order_item = OrderItem::find()
            ->where(['order_id' => $this->order->id])
            ->orderBy(['id' => SORT_ASC])
            ->limit(1)
            ->one();
        $product_id = $first_order_item->product_id;

        // 报单区
        if (ProductAreaList::checkInArea($product_id, Product::$areaMap['REPORT'])) {
            $this->upPrice();
            $this->upChain();
        }

    }

    public function upPrice()
    {
        $amount = $this->order->total_pay - $this->order->total_freight;

        // 董事
        if ($amount == $this->set->price5) {
            if ($this->user->v5 < UserLevel::$keys['vip9']) {
                $this->user->v5 = UserLevel::$keys['vip9'];
                // level 保存最高的等级 [vip1 - vip10]
                if ($this->user->level < UserLevel::$keys['vip9']) {
                    $this->user->level = UserLevel::$keys['vip9'];
                }
                $this->user->save();
            }
        }
        // CEO
        else if ($amount == $this->set->price4) {
            if ($this->user->v4 < UserLevel::$keys['vip7']) {
                $this->user->v4 = UserLevel::$keys['vip7'];
                if ($this->user->level < UserLevel::$keys['vip7']) {
                    $this->user->level = UserLevel::$keys['vip7'];
                }
                $this->user->save();
            }
        }
        // 总裁
        else if ($amount == $this->set->price3) {
            if ($this->user->v3 < UserLevel::$keys['vip5']) {
                $this->user->v3 = UserLevel::$keys['vip5'];
                if ($this->user->level < UserLevel::$keys['vip5']) {
                    $this->user->level = UserLevel::$keys['vip5'];
                }
                $this->user->save();
            }
        }
        // 总代
        else if ($amount == $this->set->price2) {
            if ($this->user->v2 < UserLevel::$keys['vip3']) {
                $this->user->v2 = UserLevel::$keys['vip3'];
                if ($this->user->level < UserLevel::$keys['vip3']) {
                    $this->user->level = UserLevel::$keys['vip3'];
                }
                $this->user->save();
            }
        }
        // 会员
        else if ($amount == $this->set->price1) {
            if ($this->user->v1 < UserLevel::$keys['vip1']) {
                $this->user->v1 = UserLevel::$keys['vip1'];
                if ($this->user->level < UserLevel::$keys['vip1']) {
                    $this->user->level = UserLevel::$keys['vip1'];
                }
                $this->user->save();
            }
        }

        // 排序
        // 找出推荐人最大的排序
        $r = C::getReferrer($this->user->id);
        if ($r) {
            $max = C::getChildrenMax($r->id);
            $this->user->sort = $max + 1;
            $this->user->save();
        } else {
            $this->user->sort = 1;
            $this->user->save();
        }
    }

    public function upChain()
    {
        $chain = C::getReferrerChain($this->user->id);

        foreach ($chain as $user) {
            if ($user->level < UserLevel::$keys['vip1']) {
                continue;
            }

            // 执行董事
            if ($user->v5 < UserLevel::$keys['vip9']) {
                $is = $this->getCount($user->id, 'v4',
                    $this->set->recommend4, $this->set->team4);
                if ($is) {
                    $user->v5 = UserLevel::$keys['vip9'];
                    $user->save();
                    $this->setLevel($user, UserLevel::$keys['vip9']);
                }
            }
            // 执行CEO
            if ($user->v4 < UserLevel::$keys['vip7']) {
                $is = $this->getCount($user->id, 'v3',
                    $this->set->recommend3, $this->set->team3);
                if ($is) {
                    $user->v4 = UserLevel::$keys['vip7'];
                    $user->save();
                    $this->setLevel($user, UserLevel::$keys['vip7']);
                }
            }
            // 执行总裁
            if ($user->v3 < UserLevel::$keys['vip5']) {
                $is = $this->getCount($user->id, 'v2',
                    $this->set->recommend2, $this->set->team2);
                if ($is) {
                    $user->v3 = UserLevel::$keys['vip5'];
                    $user->save();
                    $this->setLevel($user, UserLevel::$keys['vip5']);
                }
            }
            // 总代
            if ($user->v2 < UserLevel::$keys['vip3']) {
                $is = $this->getCount($user->id, 'v1',
                    $this->set->recommend1, $this->set->team1);
                if ($is) {
                    $user->v2 = UserLevel::$keys['vip3'];
                    $user->save();
                    $this->setLevel($user, UserLevel::$keys['vip3']);
                }
            }

            // 2+1 董事
            if ($user->v5 < UserLevel::$keys['vip10']) {
                $count = count(C::getChildren($user->id, 'v5'));
                if ($count >= 2) {
                    $user->v5 = UserLevel::$keys['vip10'];
                    $user->save();
                    $this->setLevel($user, UserLevel::$keys['vip10']);
                }
            }
            // 2+1 CEO
            if ($user->v4 < UserLevel::$keys['vip8']) {
                $count = count(C::getChildren($user->id, 'v4'));
                if ($count >= 2) {
                    $user->v4 = UserLevel::$keys['vip8'];
                    $user->save();
                    $this->setLevel($user, UserLevel::$keys['vip8']);
                }
            }
            // 2+1 总裁
            if ($user->v3 < UserLevel::$keys['vip6']) {
                $count = count(C::getChildren($user->id, 'v3'));
                if ($count >= 2) {
                    $user->v3 = UserLevel::$keys['vip6'];
                    $user->save();
                    $this->setLevel($user, UserLevel::$keys['vip6']);
                }
            }
            // 2+1 全球总代
            if ($user->v2 < UserLevel::$keys['vip4']) {
                $count = count(C::getChildren($user->id, 'v2'));
                if ($count >= 2) {
                    $user->v2 = UserLevel::$keys['vip4'];
                    $user->save();
                    $this->setLevel($user, UserLevel::$keys['vip4']);
                }
            }
            // 2+1 明星代言
            if ($user->v1 < UserLevel::$keys['vip2']) {
                $count = count(C::getChildren($user->id, 'v1'));
                if ($count >= 2) {
                    $user->v1 = UserLevel::$keys['vip2'];
                    $user->save();
                    $this->setLevel($user, UserLevel::$keys['vip2']);
                }
            }

        }
    }

    // 直推与团队人数
    protected function getCount($user_id, $recommend_level, $recommend_count, $team_count)
    {
        $r_count = C::getChildren($user_id, $recommend_level, true);
        if (count($r_count) < $recommend_count) {
            return false;
        }

        $t_count = 0;
        $team = C::getChildrenAll($user_id);
        foreach ($team as $item) {
            if ($item['level'] < UserLevel::$keys['vip1']) {
                continue;
            }
            $t_count++;
        }
        if ($t_count < $team_count) {
            return false;
        }

        return true;
    }

    // 原始等级设置
    protected function setLevel($user, $level)
    {
        if ($user->level < $level) {
            $user->level = $level;
            $user->save();
        }
    }
}